<template>
  <div>

    <!--     搜索条件-->
    <SearchForm @search="getTableData" :isSearchDisabled="isSearchDisabled" @reset="resetSearch">
      <el-col :span="8" :xs="24" v-if="$permiss[520]">
        <el-form-item :label="$t('所属租户')" prop="adminUserId">
          <el-select v-model="page.adminUserId" @change="tenantChange" style="width: 100%" clearable>
            <el-option v-for="item in tenantList" :key="item.adminUserId" :label="item.adminName"
                       :value="item.adminUserId"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('订单编号')" prop="loanRecordId">
          <el-input v-model="page.loanRecordId"/>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('用户姓名')" prop="name">
          <el-input v-model="page.name"/>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('手机号码')" prop="phone">
          <el-input v-model="page.phone"/>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('身份证')">
          <el-input v-model="page.idcard" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8" :xs="24">
        <el-form-item :label="$t('APP名称')" prop="productName">
          <el-select style="width: 100%" v-model="page.productName" clearable>
            <el-option v-for="item in productList" :key="item.productId" :label="item.appName"
                       :value="item.productName"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form-item :label="$t('实还时间')" prop="timeValue">
          <el-date-picker
              v-model="page.timeValue"
              style="width: 100%"
              type="daterange"
              value-format="yyyy-MM-dd"
              unlink-panels
              range-separator="~"
              :start-placeholder="$t('开始日期')"
              :end-placeholder="$t('结束日期')">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </SearchForm>

    <el-card shadow="never" style="margin-top: 10px">
      <el-row :gutter="24" style="margin: 0 0 10px 0">
        <el-col :span="20">
          <el-link type="primary" :underline="false" style="font-size: 18px">{{ $t('豁免总金额') }} : {{
              amountTotal
            }}
          </el-link>
        </el-col>
        <el-col :span="4" style="text-align: right;" v-show="$permiss[786]">
          <el-button type="primary" @click="exportsExcel" :loading="exportsLoading">{{ $t('导出') }}</el-button>
        </el-col>
      </el-row>
      <Table :page="page" :table-data="tableData" :table-column="tableColumn" @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange">
        <template slot-scope="{row}" slot="certificate">
          <el-image v-if="row.certificate" style=" width: 100%; height: 100px" :src="row.certificate"
                    :preview-src-list="[row.certificate]"></el-image>
        </template>
      </Table>
    </el-card>
  </div>
</template>

<script>
import {export_json_to_excel} from "@/utils/exportExcel";
import {defaultTimeDate} from '@/utils/tools'
import {selectProduct, selectTenant} from "@/api/system";
import {waiverPage, waiverTotalAmount, waiverExport} from '@/api/finance'
import {dateFormatSection} from '@/utils/tools'

export default {
  name: 'lateExemption',
  data() {
    return {
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        timeValue: defaultTimeDate(14)
      },
      tenantList: [],
      productList: [],
      tableColumn: [
        {label: this.$t('订单编号'), fixed: "left", align: "center", prop: 'loanRecordId', width: 150},
        {label: this.$t('所属租户'), align: "center", prop: 'adminUserName', isHide: !this.$permiss[520]},
        {label: this.$t('用户姓名'), prop: 'name', width: 150},
        {label: this.$t('手机号码'), prop: 'phone',},
        {label: this.$t('身份证'), prop: "idCard", align: "center", width: 100,},
        {label: this.$t('应还时间'), prop: 'refundDate', width: 150},
        {label: this.$t('实还时间'), prop: 'refundTime', width: 150},
        {label: this.$t('豁免金额'), prop: 'waiverAmount',},
        {label: this.$t('豁免天数'), prop: 'waiverDay', width: 150},
        {label: this.$t('凭证'), prop: 'certificate', slot: 'certificate', width: 100},
        {label: this.$t('备注'), prop: 'remarks',},
        {label: this.$t('APP名称'), prop: 'appName', width: 150},
        {label: this.$t('豁免操作员'), prop: 'inputName',},
        {label: this.$t('操作时间'), prop: 'inputTime', width: 150},
      ],
      tableData: [],
      amountTotal: 0,
      exportsLoading: false,
      isSearchDisabled: false
    }
  },

  mounted() {
    this.getTableData()
    selectProduct().then(res => {
      this.productList = res.data
    })
    selectTenant().then(res => {
      this.tenantList = res.data
    })
  },
  methods: {
    async getTableData() {
      this.loading = true
      this.isSearchDisabled = true
      const [startTime, endTime] = dateFormatSection(this.page.timeValue)
      const params = {
        startTime, endTime,
        ...this.page
      }
      delete params.timeValue
      this.getAmountTotal(params)
      const res = await waiverPage(params)
      if (res.code === 1) {
        const {list, total} = res.data
        this.tableData = list
        this.loading = false
        this.isSearchDisabled = false
        this.page.total = total
      } else {
        this.loading = false
        this.isSearchDisabled = false
      }
    },
    async getAmountTotal(params) {
      const res = await waiverTotalAmount(params)
      if (res.code === 1 && res.data) {
        this.amountTotal = res.data
      }
    },
    tenantChange(value) {
      selectProduct({adminUserId: value}).then(res => {
        this.productList = res.data
        this.$set(this.page,'productName', null)
      })
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    resetSearch() {
      this.page = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.getTableData()
    },
    // 导出excel
    async exportsExcel() {
      this.exportsLoading = true;
      const [startTime, endTime] = dateFormatSection(this.page.timeValue || [])
      const params = {
        ...this.page,
        startTime, endTime
      };
      delete params.timeValue
      const res = await waiverExport(params);
      if (res.code === 1) {
        const headerData = [...this.tableColumn].filter(item => {
          const result = res.data.filter(i => {
            //过滤没有权限的字段
            return typeof i[item.prop] !== "undefined"
          })
          return result.length !== 0
        })
        const column = headerData.map(item => item.label)
        const filterVal = headerData.map(item => item.prop)
        const list = [...res.data].map((v) => filterVal.map((j) => v[j]));
        const fileName = this.$t("逾期豁免表");
        export_json_to_excel({
          header: column,
          data: list,
          filename: fileName,
        });
        this.exportsLoading = false;
      } else {
        this.exportsLoading = false;
      }
    },
  }
}
</script>

<style scoped>

</style>
